import 'package:easy_chat_app/commons/db/DBUtils.dart';
import 'package:easy_chat_app/commons/db/repository/LocalConversationRepository.dart';
import 'package:easy_chat_app/commons/db/repository/LocalMessageRepository.dart';
import 'package:easy_chat_app/commons/model/Conversation.dart';
import 'package:easy_chat_app/commons/utils/HttpUtils.dart';
import 'package:logger/logger.dart';

class LocalConversationService {
  static Logger logger = Logger();

  static Future<LocalConversation> syncConversation2Local(
      int type, int? friendUserId, int? chatGroupId) async {
    Conversation conversation = (await HttpUtils.getOrCreateConversation(
            LocalConversationTypeConstants.general, friendUserId, chatGroupId))
        .data!;

    LocalConversation? localConversation =
        await getLocalConversationById(conversation.id!);
    if (localConversation != null) {
      return localConversation;
    }

    localConversation = LocalConversation.create(
        id: conversation.id,
        uid1: conversation.uid1,
        uid2: conversation.uid2,
        chatGroupId: conversation.chatGroupId,
        type: conversation.type,
        updateTime: DateTime.now().millisecondsSinceEpoch,
        unreadMsgCount: 0);
    await addLocalConversation(localConversation);
    return localConversation;
  }

  static Future<void> addLocalConversation(
      LocalConversation conversation) async {
    await DBUtils.getDB().transaction((transaction) async {
      await LocalConversationRepository.addLocalConversation(
          transaction, conversation);
    });
  }

  static Future<void> setNewestMsg(
      int? id, int updateTime, String newestMsg, int newestMsgType) async {
    await DBUtils.getDB().transaction((transaction) async {
      await LocalConversationRepository.setNewestMsg(
          transaction, id, updateTime, newestMsg, newestMsgType);
    });
  }

  static Future<void> setUnReadMsgCount(Map<int, int> unreadMsgCountMap) async {
    await DBUtils.getDB().transaction((transaction) async {
      await LocalConversationRepository.setUnReadMsgCount(
          transaction, unreadMsgCountMap);
    });
  }

  /// 批量更新会话的未读消息数量
  static Future<void> batchUpdateUnReadMsgCount(
      List<int> conversionIdList) async {
    await DBUtils.getDB().transaction((transaction) async {
      Map<int, int> unreadMsgCountMap =
          await LocalMessageRepository.getUnreadMsgCount(
              transaction, conversionIdList);
      await LocalConversationRepository.setUnReadMsgCount(
          transaction, unreadMsgCountMap);
    });
  }

  static Future<LocalConversation?> getLocalConversationById(int id) async {
    return await DBUtils.getDB().transaction((transaction) async {
      return await LocalConversationRepository.getLocalConversationById(
          transaction, id);
    });
  }

  static Future<List<LocalConversation>> listAllLocalConversation() async {
    return await DBUtils.getDB().transaction((transaction) async {
      return await LocalConversationRepository.listAllLocalConversation(
          transaction);
    });
  }

  /// 通过会话ID删除聊天记录
  static Future<void> deleteChatHistoryByConversationID(
      int conversationId) async {
    await DBUtils.getDB().transaction((transaction) async {
      await LocalConversationRepository.deleteLocalConversation(
          transaction, conversationId);
      await LocalMessageRepository.deleteByLocalConversationID(
          transaction, conversationId);
    });
  }
}
